/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.util;

import java.util.HashMap;
import java.util.Map;

/**
 * <b>(SafeUtil)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/11/18
 */
public class SafeUtil {

    /**
     * 最大密码错误次数
     */
    public static final  int                DEFAULT_FAIL_TIMES    = 5;
    /**
     * 最大锁定时间24h
     */
    public static final  long               DEFAULT_MAX_LOCK_TIME = 24 * 60 * 60L;
    /**
     * 最小锁定时间5m
     */
    public static final  long               DEFAULT_MIN_LOCK_TIME = 5 * 60L;
    /**
     * 密码错误次数和它对应的锁定时间映射，单位秒
     */
    private static final Map<Integer, Long> LOCK_TIME_MAP         = new HashMap<>(12);
    private static final String             HOUR                  = "%s 小时";
    private static final String             MINUTE                = "%s 分钟";

    static {
        LOCK_TIME_MAP.put(5, 5 * 60L);
        LOCK_TIME_MAP.put(6, 30 * 60L);
        LOCK_TIME_MAP.put(7, 60 * 60L);
        LOCK_TIME_MAP.put(8, 3 * 60 * 60L);
        LOCK_TIME_MAP.put(9, 5 * 60 * 60L);
        LOCK_TIME_MAP.put(10, 24 * 60 * 60L);
    }

    public static long getLockTime(int errorTimes) {
        return LOCK_TIME_MAP.getOrDefault(errorTimes, DEFAULT_MAX_LOCK_TIME);
    }

    public static long reduce(long value) {
        return value / 60 + (value % 60 <= 20 ? 0 : 1);
    }

    public static String format(long time) {
        long minute = reduce(time);

        if (minute < 60) {
            return String.format(MINUTE, minute);
        }

        long more = minute % 60;

        return String.format(HOUR, minute / 60) + (more == 0 ? "" : String.format(MINUTE, more));
    }

}
